package ziv.map;

import java.util.ArrayList;

import ziv.map.activity.Config;
import ziv.map.activity.Edition;
import ziv.map.activity.MeActivity;
import ziv.map.activity.OtherPeopleActivity;
import ziv.map.activity.UploadLonLatService;
import ziv.map.R;
import ziv.map.utils.SqliteDB;
import ziv.utils.html.FileAndroid;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.PoiOverlay;
import com.baidu.mapapi.RouteOverlay;

import android.content.Intent;
import android.graphics.Point;

import android.os.Bundle;

import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class mapMain extends MapActivity {
	BMapManager mBMapMan = null;
	MapView mMapView = null; // 地图视图
	MapController mMapController = null; // 控制和驱动平移和缩放
	int requestCode = 0; // 选择要定位的人后，的返回标识。

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		
		// 初始化数据库
		SqliteDB SqliteDB = new SqliteDB(this, "observeDB");
		SqliteDB.createDatabase();

		// 初始化地图
		mBMapMan = new BMapManager(getApplication());
		mBMapMan.init("A58A5DCAED6D0D22FEF67D023206D4116DB9D1AA", null);
		super.initMapActivity(mBMapMan);
		mMapView = (MapView) findViewById(R.id.bmapsView);
		mMapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件
		mMapController = mMapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
		GeoPoint point = new GeoPoint((int) (39.915 * 1E6),
				(int) (116.404 * 1E6)); // 用给定的经纬度构造一个GeoPoint，单位是微度 (度 * 1E6)
		mMapController.setCenter(point); // 设置地图中心点
		mMapController.setZoom(12); // 设置地图zoom级别
		
		
		

		

	}

	@Override
	protected void onDestroy() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (mBMapMan != null) {
			mBMapMan.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (mBMapMan != null) {
			mBMapMan.start();
		}
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		// 启动上报服务 
		//UploadLonLatService uploadLonLatService=new UploadLonLatService();
		Intent intent=new Intent(mapMain.this,UploadLonLatService.class);
		startService(intent);
//		uploadLonLatService.mapbaiduDemo=this;
//		Thread thread=new Thread(threadU);
//		thread.start();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(1, 1, 1, "我的位置").setIcon(R.drawable.menu);
		menu.add(1, 2, 2, "实时交通").setIcon(R.drawable.menu2);
		menu.add(1, 3, 3, "亲人位置").setIcon(R.drawable.menu);
		menu.add(1, 4, 4, "允许谁看").setIcon(R.drawable.menu);

		// menu.add(1, 4, 4, "测试条目覆盖");
		menu.add(1, 5, 5, "周边搜索").setIcon(R.drawable.menu);
		menu.add(1, 6, 6, "版本信息");
		//menu.add(1, 7, 7, "设置").setIcon(R.drawable.menu);
		// menu.add(1, 7, 7, "公交换乘");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == 1) {
			// 初始化Location模块
			MKLocationManager mLocationManager = mBMapMan.getLocationManager();
			// 通过enableProvider和disableProvider方法，选择定位的Provider
			// mLocationManager.enableProvider(MKLocationManager.MK_NETWORK_PROVIDER);
			// mLocationManager.disableProvider(MKLocationManager.MK_NETWORK_PROVIDER);

			// 添加定位图层
			MyLocationOverlay mylocTest = new MyLocationOverlay(this, mMapView);
			mylocTest.enableMyLocation(); // 启用定位 . 这个可能是gps和network一起用
			mylocTest.enableCompass(); // 启用指南针10.
			System.out.println("lat="
					+ mylocTest.getMyLocation().getLatitudeE6());
			System.out.println("lon="
					+ mylocTest.getMyLocation().getLongitudeE6());
			mMapView.getOverlays().add(mylocTest);

			// 设置中心点和级别
			mMapController.setCenter(mylocTest.getMyLocation()); // 设置地图中心点
			mMapController.setZoom(15); // 设置地图zoom级别
		} else if (item.getItemId() == 2) {
			mMapView.setTraffic(true); // 设置实时交通
		} else if (item.getItemId() == 3) {// 选择要定位的人

			Intent intent = new Intent(mapMain.this,
					OtherPeopleActivity.class);
			startActivityForResult(intent, requestCode);
			// startActivity(intent);
		} else if (item.getItemId() == 4) {
			Intent intent = new Intent(mapMain.this, MeActivity.class);
			startActivity(intent);
		} else if (item.getItemId() == 5) {

			// 实例化搜索
			MKSearch mMKSearch = new MKSearch();
			// mMKSearch.init(mBMapMan, new mapBaiduDemo());
			mMKSearch.init(mBMapMan, new MKSearchListener() {

				public void onGetPoiResult(MKPoiResult res, int type, int error) {
					// 错误号可参考MKEvent中的定义
					if (error != 0 || res == null) {
						Toast.makeText(mapMain.this, "抱歉，未找到结果",
								Toast.LENGTH_LONG).show();
						return;
					}
					// 将地图移动到第一个POI中心点
					if (res.getCurrentNumPois() > 0) {
						// 将poi结果显示到地图上
						PoiOverlay poiOverlay = new PoiOverlay(
								mapMain.this, mMapView);
						poiOverlay.setData(res.getAllPoi());
						mMapView.getOverlays().clear();
						mMapView.getOverlays().add(poiOverlay);
						mMapView.invalidate();
						mMapView.getController().animateTo(res.getPoi(0).pt);
					} else if (res.getCityListNum() > 0) {
						String strInfo = "在";
						for (int i = 0; i < res.getCityListNum(); i++) {
							strInfo += res.getCityListInfo(i).city;
							strInfo += ",";
						}
						strInfo += "找到结果";
						Toast.makeText(mapMain.this, strInfo,
								Toast.LENGTH_LONG).show();
					}
				}

				public void onGetDrivingRouteResult(MKDrivingRouteResult res,
						int error) {
				}

				public void onGetTransitRouteResult(MKTransitRouteResult res,
						int error) {
				}

				public void onGetWalkingRouteResult(MKWalkingRouteResult res,
						int error) {
				}

				public void onGetAddrResult(MKAddrInfo res, int error) {
				}

			});
			// 开始搜索
			mMKSearch.poiSearchNearBy("饭店", mMapView.getMapCenter(), 5000);

		} else if (item.getItemId() == 6) {
			Intent intent = new Intent(mapMain.this, Edition.class);
			startActivity(intent);
		} else if (item.getItemId() == 7) {
			Intent intent = new Intent(mapMain.this, Config.class);
			intent.putExtra("config", true);
			startActivity(intent);
		} else if (item.getItemId() == 17) {

			// 实例化搜索
			MKSearch mMKSearch = new MKSearch();
			// mMKSearch.init(mBMapMan, new mapBaiduDemo());
			mMKSearch.init(mBMapMan, new MKSearchListener() {
				@Override
				public void onGetAddrResult(MKAddrInfo result, int iError) {
				}

				@Override
				public void onGetDrivingRouteResult(
						MKDrivingRouteResult result, int iError) {
					// Log.d("ziv", "aaaaaaaaaaaa");
					if (result == null) {
						return;
					}
					RouteOverlay routeOverlay = new RouteOverlay(
							mapMain.this, mMapView);
					// 此处仅展示一个方案作为示例
					routeOverlay.setData(result.getPlan(0).getRoute(0));
					mMapView.getOverlays().add(routeOverlay);

				}

				@Override
				public void onGetPoiResult(MKPoiResult result, int type,
						int iError) {
				}

				@Override
				public void onGetTransitRouteResult(
						MKTransitRouteResult result, int iError) {
				}

				@Override
				public void onGetWalkingRouteResult(
						MKWalkingRouteResult result, int iError) {
				}
			});

			MKPlanNode start = new MKPlanNode();
			start.pt = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6));
			MKPlanNode end = new MKPlanNode();
			end.pt = new GeoPoint(40057031, 116307852);
			// 设置驾车路线搜索策略，时间优先、费用最少或距离最短
			mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
			mMKSearch.drivingSearch(null, start, null, end);

		} else if (item.getItemId() == 13) {
			GeoPoint geoPoint = new GeoPoint((int) (39.915 * 1E6),
					(int) (116.404 * 1E6));
			Point point = mMapView.getProjection().toPixels(geoPoint, null);
			mMapController.setCenter(geoPoint);

			MyOverlay myOverLay = new MyOverlay();
			myOverLay.setPoint(point);
			mMapView.getOverlays().clear();
			mMapView.getOverlays().add(myOverLay);// 添加标注点

		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		// super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == this.requestCode) {
			if (resultCode == RESULT_OK) {
				// Toast.makeText(getApplicationContext(),
				// data.getExtras().getCharSequence("mobile")
				// ,Toast.LENGTH_SHORT).show();
				String mobileStr = data.getExtras().getCharSequence("mobile")
						.toString();
				String latStr = data.getExtras().getCharSequence("lat")
						.toString();
				String lonStr = data.getExtras().getCharSequence("lon")
						.toString();
				System.out.println("lat=" + latStr + "lon=" + lonStr);
				int latInt=Integer.valueOf(latStr);
				int lonInt=Integer.valueOf(lonStr);
				// 添加标注
				GeoPoint geoPoint = new GeoPoint(lonInt,
						latInt);
				// poi 点overlay
				PoiOverlay poioverlay = new PoiOverlay(this, mMapView);
				java.util.ArrayList<MKPoiInfo> dataPoi = new ArrayList<MKPoiInfo>();
				MKPoiInfo mkPoiInfo = new MKPoiInfo();
				mkPoiInfo.ePoiType = 0;
				mkPoiInfo.pt = geoPoint; // 设置poi的位置
				mkPoiInfo.name = mobileStr; // 设置poi显示的信息
				dataPoi.add(mkPoiInfo);

				poioverlay.setData(dataPoi);
				mMapView.getOverlays().add(poioverlay);
				mMapController.setCenter(geoPoint); // 设置中心点

			}
		}
	}
Runnable threadU=new Runnable(){

	@Override
	public void run() {
		Intent intent=new Intent(mapMain.this,UploadLonLatService.class);
		startService(intent);
	}
	
};
//	@Override
//	public void run() {
//		// 启动上报服务
//		Intent intent=new Intent(mapBaiduDemo.this,UploadLonLatService.class);
//		startService(intent);
//	}

	// MKSearchListener mKSearchListener=;
}